@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

:: bootstrap
pushd %~dp0
call _configure.bat
popd

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Constants:
::	
::	SVN_CLIENT_CLI - PATH to SVN client: "svn"
::	
::	SVN_SCHEMA_FILES - DB2 SCHEMA for the Suite application: "db2admin", "db2inst"
::	
::	AUTO_DEFINE - set to "TRUE" to use defaults in connecting to SVN Hosting
::	
::	SVN_BRANCH_NAME - Branch name or directory name of the branch to create inside the SVN Server Repository
::	
::	SVN_BRANCH_MESSAGE - The Message Log to use when creating new Branch inside the SVN Server Repository
::	
::	SVN_HOST - IP Address or Hostname hosting the SVN Server Repository: [default: "jabberwock"]
::	
::	SVN_TRUNK_URL - Relative URL of SVN Server Repository to access "trunk" directory: [default: "suite/releases"]
::	
::	SVN_BRANCH_URL - Relative URL of SVN Server Repository to access "branch" directory: [default: "suite/branches-releases"]
::	
::	SVN_USER - Username to Access SVN Server Repository: [default: "miches"]
::	
::	SVN_PASS - Password to Access SVN Server Repository: [default: "mike"]
::	
::	SVN_CHECKOUT_DIR - Local File System location of your working copy after creating your new Branch: [format: "DRIVE:\PATH\ETC.."]
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

set CMDDIR=%~dp0
set SVN_SCHEMA_FILES=db2admin
set AUTO_DEFINE=TRUE

set SVN_BRANCH_NAME=%1
set SVN_BRANCH_MESSAGE=%2

set SVN_CHECKOUT_DIR=%BRANCHES_DIR%

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Fix Configurations
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

if not defined SVN_BRANCH_NAME (
	call :define_var SVN_BRANCH_NAME Enter SVN Branch Name
)

if not defined SVN_BRANCH_MESSAGE (
	call :define_var SVN_BRANCH_MESSAGE Enter SVN Branch Revision Message
)

if not defined AUTO_DEFINE (

	call :define_var SVN_HOST Enter SVN Branch Name
	call :define_var SVN_TRUNK_URL Enter Trunk Url Path
	call :define_var SVN_BRANCH_URL Enter Branch Url Path
	call :define_var SVN_USER Enter Username
	call :define_var SVN_PASS Enter Password
)

if not defined SVN_CHECKOUT_DIR (
	call :define_dir_var SVN_CHECKOUT_DIR Enter Check out dir
)

if not defined SVN_BRANCH_DIR (
	set SVN_BRANCH_DIR=%SVN_USER%
)

set SVN_FULL_CHECKOUT_DIR=%SVN_CHECKOUT_DIR%\%SVN_BRANCH_NAME%

if not exist %SVN_FULL_CHECKOUT_DIR%\nul (
	
	mkdir %SVN_FULL_CHECKOUT_DIR%
	
)



:: finalize trunk and branch url
set SVN_FULL_TRUNK_URL=%SVN_SCHEME%://%SVN_HOST%/%SVN_TRUNK_URL%
set SVN_FULL_BRANCH_URL=%SVN_SCHEME%://%SVN_HOST%/%SVN_BRANCH_URL%/%SVN_BRANCH_DIR%/%SVN_BRANCH_NAME%



:: check if SVN Client is available in %PATH%
echo Checking Available SVN Client ...
call :executable_exists %SVN_CLIENT_CLI%
if defined batch_error (
	echo %batch_error%
	echo SVN Client do not exist.
	goto :end
)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Summary
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
cls
echo Creating Branch Using values below:
echo.
echo Branch: %SVN_BRANCH_NAME%
echo Create Branch Message: %SVN_BRANCH_MESSAGE%
echo Host: %SVN_HOST%
echo Trunk Url: %SVN_TRUNK_URL%
echo Full Trunk Url: %SVN_FULL_TRUNK_URL%
echo Branch Url: %SVN_BRANCH_URL%
echo New Branch Url: %SVN_FULL_BRANCH_URL%
echo SVN User: %SVN_USER%
echo Checkout Directory: %SVN_FULL_CHECKOUT_DIR%
pause

::goto :end

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: SVN Branch Out and Check out
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

echo svn cp -m "%SVN_BRANCH_MESSAGE%" %SVN_FULL_TRUNK_URL% %SVN_FULL_BRANCH_URL% --username %SVN_USER% --password %SVN_PASS%
call svn cp -m "%SVN_BRANCH_MESSAGE%" %SVN_FULL_TRUNK_URL% %SVN_FULL_BRANCH_URL% --username %SVN_USER% --password %SVN_PASS%
if not %errorlevel% == 0 (
	echo failed in branching out from SVN server.
	goto :end
)

echo svn checkout %SVN_FULL_BRANCH_URL% %SVN_FULL_CHECKOUT_DIR% --username %SVN_USER% --password %SVN_PASS%
call svn checkout %SVN_FULL_BRANCH_URL% %SVN_FULL_CHECKOUT_DIR% --username %SVN_USER% --password %SVN_PASS%
if not %errorlevel% == 0 (
	echo failed in checking out from SVN server to Working copy.
	goto :end
)


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Apply Configuration files by DB2 Schema
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
if not defined NO_BRANCH_BOOTSTRAP (
	set source=%CMDDIR%files\%SVN_SCHEMA_FILES%\*
	set target=%SVN_FULL_CHECKOUT_DIR%

	echo.
	echo Applying Schema Configuration to Working Copy %SVN_CHECKOUT_DIR%
	echo. 
	xcopy /G /Y /R /E %source% %target%

	echo.
	echo Dont forget to SVN Commit your Configuration updates!
	echo.
	echo.
	
) else (
	echo.
	echo Completed checkout of your working copy at [ %SVN_FULL_CHECKOUT_DIR% ]
	echo.
	echo.

)

:end
echo.
echo Exiting...
echo.
endlocal & set _error=%batch_error%

if defined _error (
	set "_error="
	exit /B 1
) else (
	exit /B 0
)






::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: SUBROUTINES:
:: 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::






::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Check if dir exists
::
:: usage: call :directory_exists [path]
:: 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:directory_exists
set "batch_error="
setlocal
if [%1] == [] (
	set _error=No Directory Specified
)

if not exist %1\nul  (
	set _error=Directory %1 do not exist!
)

endlocal & call set batch_error=%_error%

if defined batch_error (
	exit /B 1
)

goto :eof

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Check if executable exists
::
:: usage: call :executable_exists [path]
:: 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:executable_exists
set "batch_error="
setlocal
if [%1] == [] (
	set _error=No Executable File defined
) else (
	call where %1
	if not [%errorlevel%] == [0]  (
		set _error=Executable File %1 do not exist
	)
)

endlocal & call set batch_error=%_error%

if defined batch_error (
	exit /B 1
)
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Directory Variable Declaration
::
:: usage: call :define_dir_var [varname] [prompt label]
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:define_dir_var
setlocal
set _dirname=%1
call set _dirdefault=%%%_dirname%%%
set _params=%*

:: check if default dir exists
call :directory_exists %_dirdefault%
if defined batch_error (
	set _dirdefault=
)

:find_dir
call :define_var %*
call set _value=%%%_dirname%%%

:: try creating directory
call :create_dir %_value%

call :directory_exists %_value%

if defined batch_error (
	echo %batch_error%
	call set %_dirname%=%_dirdefault%
	goto :find_dir
)
endlocal & call set %_dirname%=%_value%

goto :eof


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Directory create
::
:: usage: call :define_var [varname] [prompt label]
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:create_dir
setlocal 
set dirname=%1
set _error=
if [%dirname%] == [] (
	set _error=No Directory specified
	goto :create_dir_end
)

call :directory_exists %dirname%

:: directory exists! exiting...
if not defined batch_error (
	goto :create_dir_end
)


echo %batch_error%

call choice /D Y /T 5 /C YN /M "Do You want to create Directory [%dirname%]"

if not [%errorlevel%] == [1] (
	set _error=Directory not created [%dirname%]
	goto :create_dir_end
)


call mkdir %dirname%

if not [%errorlevel%] == [0] (
	set _error=Unable to create directory [%dirname%]
	goto :create_dir_end
)

:: generate error
call :directory_exists %dirname%

if defined batch_error (
	set _error=%batch_error%
)


:create_dir_end

endlocal & call set batch_error=%_error%
goto :eof



::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Variable Declaration
::
:: usage: call :define_var [varname] [prompt label]
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:define_var
setlocal
set _name=%1
call set _default=%%%_name%%%
set _value=%_default%
shift
set _label=%1
:define_var_label_loop
shift
if not [%1]==[] (
	set _label=%_label% %1
	goto :define_var_label_loop
)

if defined _default (
	set _label=%_label% [default: %_default%]
)

:define_var_value_loop
set /p _value=%_label%: 
if not defined _value (
	echo required value: %_name%
	if not defined _default (
		goto :define_var_value_loop
	) else (
		set _value=%_default%
	)
)
::call set %_name%=%_value%
endlocal & call set %_name%=%_value%
goto :eof


